
# 插入数据库
import sqlite3
import traceback


def ExeSqlliteList(sqllitename,sqlList):
    """
    该函数和上面一样执行一个sql列表且不返回结果
    属于插入和更新类函数 但该函数针对db3
    :param sqllitename:
    :param sqlList:
    :return:
    """
    dbMsg = None
    conn = sqlite3.connect(sqllitename)  # 'ctgu_adksvideo.db3'
    cur = conn.cursor()
    failed_count = success_count =0
    if conn:
        for sql in sqlList:
            try:
                cur.execute(sql)
                success_count += 1
            except:
                print('*errSql:' + sql)
                dbMsg = '*InsertError:' + traceback.format_exc()
                failed_count += 1
            if dbMsg:
                print(dbMsg)
                continue
    conn.commit()
    conn.close()
    return success_count, failed_count


def ExeSqlliteSql(sqllitename,sql):
    """
    该函数和上面一样执行一个sql列表且不返回结果
    属于插入和更新类函数 但该函数针对db3
    :param sqllitename:
    :param sqlList:
    :return:
    """
    dbMsg = None
    conn = sqlite3.connect(sqllitename)  # 'ctgu_adksvideo.db3'
    cur = conn.cursor()
    if conn:
        try:
            print(sql)
            cur.execute(sql)
        except:
            print('*errSql:' + sql)
            dbMsg = '*InsertError:' + traceback.format_exc()
        if dbMsg:
            print(dbMsg)
            return False
    else:
        return False
    conn.commit()
    conn.close()
    return True



def SelectFromSqllite(sqllitename,sql):
    """
    该函数和上面一样执行一个sql列表且不返回结果
    属于插入和更新类函数 但该函数针对db3
    :param sqllitename:
    :param sqlList:
    :return:
    """
    dbMsg = None
    conn = sqlite3.connect(sqllitename)  # 'ctgu_adksvideo.db3'
    cur = conn.cursor()
    if conn:
        try:
            print(sql)
            cur.execute(sql)
            rows = cur.fetchall()
            return rows
        except:
            print('*errSql:' + sql)
            dbMsg = '*InsertError:' + traceback.format_exc()
        if dbMsg:
            print(dbMsg)
            return False
    else:
        return False
    conn.commit()
    conn.close()

def sqliteEscape(keyWord):
    keyWord = keyWord.replace("'", "''")
    keyWord = keyWord.replace("\\", "\\\\")
    return keyWord


